Ext.namespace("Ext.ux.sysmeta")

Ext.ux.sysmeta.TableTreePanel = Ext.extend(Ext.tree.TreePanel,{

	//自定义属性
	url:'',
	
	//父类属性初始化
	userArrows:true,
	animate:true,
	enableDD:true,
	border:false,
	rootVisible:true,
	autoScroll:true,
	containerScroll:true,
	bodyStyle:'padding:3px',
	
	
	//组件初始化
	initComponent:function(){
		this.root = new Ext.tree.AsyncTreeNode({
			id:'-1',
			text:'元数据树',
			draggable:false,
			iconcls:'meta-root'
		});
		
		this.loader = new Ext.tree.TreeLoader({
			dataUrl:this.url,
			listeners:{
				'beforeload':function(treeLoader, node){
					treeLoader.baseParams.nodeId = node.attributes.id;
				}
			}
		});
		// 定义用户节点的右键菜单
		var ownerMenu = new Ext.menu.Menu({
			items: [{
				text: '新增表',
				iconCls: 'add-child-org',
				handler:this.addTable,
				scope: this
			},{
				text: '刷新',
				iconCls: 'refresh',
				handler:this.refresh,
				scope: this
			}]
		});
		
		// 定义用户节点的右键菜单
		var tableMenu = new Ext.menu.Menu({
			items: [{
				text: '修改表信息',
				iconCls: 'add-child-org',
				handler:this.updateTable,
				scope: this
			},{
				text: '删除表',
				iconCls: 'delete-org ',
				handler:this.deleteTable,
				scope: this
			},{
				text: '新增字段',
				iconCls: 'add-position',
				handler:this.addColumn,
				scope: this
			},{
				text: '刷新',
				iconCls: 'refresh',
				handler:this.refresh,
				scope: this
			}]
		});
		
		// 定义用户节点的右键菜单
		var columnMenu = new Ext.menu.Menu({
			items: [{
				text: '修改字段信息',
				iconCls: 'add-position',
				handler:this.updateColumn,
				scope: this
			},{
				text: '删除字段',
				iconCls: 'delete-position',
				handler:this.deleteColumn,
				scope: this
			}]
		});
		
		//根据isDisplayMenu配置项判断需要注册右键菜单
		this.on('contextmenu', function(node, e){			
			e.preventDefault();
			node.select();
					
			//注册节点右键菜单
			if(node.attributes.id.indexOf('owner')>=0){
					ownerMenu.showAt(e.getXY());
			}
			else if(node.attributes.id.indexOf('table')>=0){
				tableMenu.showAt(e.getXY());
			}
			else if(node.attributes.id.indexOf('column')>=0){
				columnMenu.showAt(e.getXY());
			}
		}, this);
		
		
		//节点双击
		this.on('beforeclick', function(node, e){
			var nodeType = node.attributes.id.split('_')[0];
			var nodeId = node.attributes.id.split('_')[1];
			
			//用户
			if(nodeType =='owner'){
				this.loadOwnerNodeInfo();
			}
			//表
			if(nodeType =='table'){
				var text = node.attributes.text;
				this.loadTableNodeInfo(nodeId,text);
			}
			//字段
			else if(nodeType =='column'){
				this.loadColumnNodeInfo(nodeId);	
			}
		}, this);
		
		
		// 调用父类初始化
		Ext.ux.sysmeta.TableTreePanel.superclass.initComponent.call(this);
	},
	
	//事件初始化
	initEnvents:function(){
		// 调用父类初始化
		Ext.ux.sysmeta.TableTreePanel.superclass.initEvents.call(this);
	},
	
	//外面调用的方法
	test:function(){
		window.alert('test');
	},
	
	loadOwnerNodeInfo:function(){
		
		var mainDetail = Ext.getCmp('mainDetail');
		mainDetail.removeAll();
		
		var tableQueryPanel = Ext.getCmp('tableQueryPanel');
		if(tableQueryPanel == null){
			tableQueryPanel = new Ext.ux.sysmeta.SysTableQueryPanel({id:'tableQueryPanel'});	
		}
		
		/*var columnQueryPanel =Ext.getCmp('columnQueryPanel');
		if(columnQueryPanel == null){
			columnQueryPanel = new Ext.ux.sysmeta.SysColumnQueryPanel({id:'columnQueryPanel'});
		}*/
		
		mainDetail.add(tableQueryPanel);
		/*mainDetail.add(columnQueryPanel);*/
		mainDetail.setActiveTab(0);
	},
	
	loadTableNodeInfo: function(tableId,tableName){
		var mainDetail = Ext.getCmp('mainDetail');
		mainDetail.removeAll();
		var tableDetailPanel = Ext.getCmp('tableDetailPanel');
		if(tableDetailPanel == null){
			tableDetailPanel = new Ext.ux.sysmeta.SysTableDetailPanel({id:'tableDetailPanel',title:'表结构信息'});
		}
		var columnGridPanel = Ext.getCmp('columnGridPanel');
		if(columnGridPanel == null){
			columnGridPanel = new Ext.ux.sysmeta.SysColumnGridPanel({id:'columnGridPanel',tableId:tableId,tableName:tableName,title:'表的字段列表'});
		}
		mainDetail.add(tableDetailPanel);
		mainDetail.add(columnGridPanel);
		mainDetail.setActiveTab(0);
		
		tableDetailPanel.loadTable(tableId);//加载数据
		columnGridPanel.loadData(tableId);
	},
	
	loadColumnNodeInfo: function(columnId){
		var mainDetail = Ext.getCmp('mainDetail');
		mainDetail.removeAll();
		
		var columnDetailPanel = Ext.getCmp('columnDetailPanel');
		if(columnDetailPanel == null){
		 	columnDetailPanel = new Ext.ux.sysmeta.SysColumnDetailPanel({columnId:columnId,title:'字段详细信息'});
		 }
		mainDetail.add(columnDetailPanel);
		mainDetail.setActiveTab(0);
		
		columnDetailPanel.loadColumn(columnId);//加载数据
	},
	
	addTable:function(){
		var node = this.getSelectionModel().getSelectedNode();
		var nodeId = node.attributes.id;
		
		if(nodeId.indexOf('owner')>=0){
			var owner = nodeId.split('_')[1];
			var tableDetailWindow = Ext.getCmp('tableDetailWindow');
			if(tableDetailWindow == null){
				tableDetailWindow = new Ext.ux.sysmeta.SysTableDetailWindow({id:'tableDetailWindow', title:'新增表',owner:owner});
				tableDetailWindow.show();
			}
		}
	},
	updateTable:function(){
		var node = this.getSelectionModel().getSelectedNode();
		var nodeId = node.attributes.id.split('_')[1];
		if(nodeId != null){
			var tableDetailWindow = Ext.getCmp('tableDetailWindow');
			if(tableDetailWindow == null){
				tableDetailWindow = new Ext.ux.sysmeta.SysTableDetailWindow({id:'tableDetailWindow', title:'修改表信息',tableId:nodeId});
				tableDetailWindow.show();
				tableDetailWindow.showTableInfo();
			}
		}
	},
	
	deleteTable:function(){
		var node = this.getSelectionModel().getSelectedNode();
		var nodeId = node.attributes.id.split('_')[1];
		if(nodeId != null){
			Ext.MessageBox.show({
			    title: '系统提示',
			    msg: '确认要删除选中的表吗？<br/><font color="red">注意</font>：删除表将删除该表下的所有字段。',
			    buttons: Ext.Msg.YESNO,
			    icon: Ext.MessageBox.QUESTION,
			    fn: function(btn){
			    	if(btn == 'yes'){
						Ext.Ajax.request({
							url:'sys-meta-table!delete.action',
							success:function(){
								this.refresh();
							},
							failure: function(form, action) {
								Ext.Msg.alert('系统提示', '删除表失败！');
							},
							params:{
								id: nodeId
							},
							waitMsg: '正在删除数据，稍候...',
							scope: this
						});
					}
				},
				width: 350,
			    scope: this
			});
		};
	},
	
	addColumn:function(){
		var node = this.getSelectionModel().getSelectedNode();
		var nodeId = node.attributes.id.split('_')[1];
		var text = node.attributes.text;
		if(nodeId != null){
			var columnDetailWindow = Ext.getCmp('columnDetailWindow');
			if(columnDetailWindow == null){
				columnDetailWindow = new Ext.ux.sysmeta.SysColumnDetailWindow({id:'columnDetailWindow',title:'增加字段',tableId:nodeId,tableName:text});
				columnDetailWindow.show();
			}
		}
	},
	
	updateColumn:function(){
		var node = this.getSelectionModel().getSelectedNode();
		var nodeId = node.attributes.id.split('_')[1];
		
		if(nodeId != null){
			var columnDetailWindow = Ext.getCmp('columnDetailWindow');
			if(columnDetailWindow == null){
				columnDetailWindow = new Ext.ux.sysmeta.SysColumnDetailWindow({id:'columnDetailWindow',columnId:nodeId,title:'修改字段信息'});
				columnDetailWindow.show();
				columnDetailWindow.showColumnInfo();
			}
		}
	},
	deleteColumn:function(){
		var node = this.getSelectionModel().getSelectedNode();
		var nodeId = node.attributes.id.split('_')[1];
		if(nodeId != null){
			Ext.MessageBox.show({
			    title: '系统提示',
			    msg: '确认要删除选中的字段吗？',
			    buttons: Ext.Msg.YESNO,
			    icon: Ext.MessageBox.QUESTION,
			    fn: function(btn){
			    	if(btn == 'yes'){
						Ext.Ajax.request({
							url:'sys-meta-column!delete.action',
							success:function(){
								this.refresh();
							},
							failure: function(form, action) {
								Ext.Msg.alert('系统提示', '删除字段失败！');
							},
							params:{
								id: nodeId
							},
							waitMsg: '正在删除数据，稍候...',
							scope: this
						});
					}
				},
				width: 350,
			    scope: this
			});
		};
	},
	refresh:function(){
		var node = this.getSelectionModel().getSelectedNode();
		if(node != null && node.attributes.id != null && node.parentNode != null){
			node.parentNode.reload();
		}else{
			this.root.reload();
		}
	}
});